﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using Common;
using Lapas.Common;
using WorkDAL.BranchCode;

namespace BranchCode
{
    public partial class BranchCodeEditForm : EditForm
    {
        public tbBranchCode branchCode = new tbBranchCode();

        public BranchCodeEditForm()
        {
            InitializeComponent();
            base.OkButton = btnOK;
            base.CloseButton = btnClose;
            btnOK.Enabled = CommonSettings.isSuperAdmin;
        }

        protected override void BindData()
        {
            if (base.FormAction != EditFormAction.Insert)
            {
                txtBranchCode.Text = branchCode.BranchCode;
                txtAddressEng.Text = branchCode.AddressEng;
                txtAddressViet.Text = branchCode.AddressViet;
                txtNameEng.Text = branchCode.NameEng;
                txtNameViet.Text = branchCode.NameViet;
                txtPhoneNumber.Text = branchCode.PhoneNumber;
                txtTaxCode.Text = branchCode.TaxCode;
            }
        }

        private void BranchCodeEditForm_Load(object sender, EventArgs e)
        {
            if (base.FormAction == EditFormAction.Change)
                txtBranchCode.Enabled = false;
            else
                txtBranchCode.Enabled = true;
        }


        protected override bool DoCreate()
        {
            try
            {
                if (!CheckData()) return false;

                tbBranchCode tbbranchCode = new tbBranchCode();

                tbbranchCode.TaxCode = txtTaxCode.Text.Trim();
                tbbranchCode.PhoneNumber = txtPhoneNumber.Text.Trim();
                tbbranchCode.NameViet = txtNameViet.Text.Trim();
                tbbranchCode.NameEng = txtNameEng.Text.Trim();
                tbbranchCode.BranchCode = txtBranchCode.Text.Trim();
                tbbranchCode.AddressViet = txtAddressViet.Text.Trim();
                tbbranchCode.AddressEng = txtAddressEng.Text.Trim();

                BranchCodeDataAccess.Instance().CreateBranchCode(tbbranchCode);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }

        private bool CheckData()
        {
            if (txtBranchCode.Text == string.Empty)
            {
                erp.SetError(txtBranchCode, "Chưa nhập mã chi nhánh");
                return false;
            }

            return true;
        }

        protected override bool DoUpdate()
        {
            try
            {
                if (!CheckData()) return false;

                tbBranchCode tbbranchCode = new tbBranchCode();

                tbbranchCode.TaxCode = txtTaxCode.Text.Trim();
                tbbranchCode.PhoneNumber = txtPhoneNumber.Text.Trim();
                tbbranchCode.NameViet = txtNameViet.Text.Trim();
                tbbranchCode.NameEng = txtNameEng.Text.Trim();
                tbbranchCode.BranchCode = txtBranchCode.Text.Trim();
                tbbranchCode.AddressViet = txtAddressViet.Text.Trim();
                tbbranchCode.AddressEng = txtAddressEng.Text.Trim();

                BranchCodeDataAccess.Instance().UpdateBranchCode(tbbranchCode);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }


    }
}
